﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace SharePoint.Contrib.Extensions
{
    /// <summary>
    /// Extension methods for the ControlsCollection class
    /// </summary>
    public static class ControlsCollectionExtensions
    {
        /// <summary>
        /// Finds all controls of a certain type in the collection.
        /// </summary>
        public static IEnumerable<T> FindControlsOfType<T>(this ControlCollection Controls) where T : class
        {

            T control;

            foreach (Control ctrl in Controls)
            {
                if ((control = ctrl as T) != null)
                {
                    yield return control;
                }

                foreach (T child in FindControlsOfType<T>(ctrl.Controls))
                {
                    yield return child;
                }
            }

        }

    }
}
